home *** CD-ROM | disk | FTP | other *** search
/ Webshot XL / WEBSHOP_XL.ISO / WebShop XL / data1.cab / Examples / Web_Examples / AppForms / WebSite / CGI-BIN / CGI3.cgi < prev    next >
Text File  |  2001-09-05  |  8KB  |  230 lines

  1. $cginame = "CGI3";
  2. $cgifolder = "";
  3. $pageURL = "../UndergraduateFrameSource.htm";
  4. $cgiurl = "\""."$cgifolder"."$cginame".".cgi"."\"";
  5. $template = "Template1";
  6. ###############################################################
  7. #             1.Environmental prefix                          #
  8. ###############################################################
  9. #                  *-OS platform -*                           #
  10. $osplat = "Win32";
  11. #             *-DB connectivity platform-*                    #
  12. $dbplat = "ODBC";
  13. ###############################################################
  14. #             2.Section of datasource properies               #
  15. ###############################################################
  16. $dbname = "ADDBOOK";
  17. ###############################################################
  18. #              3.Section of columns                           #
  19. ###############################################################
  20. $command = INSERT;
  21. @columns = ( 'FaxNumber','WorkPhone','EmailAddress','Country','StateOrProvince','City','Address','FullName','Message','TuitionPlan','speciality');
  22. @captions = ( 'Fax Name','Work Phone','Email Address','Country','State or Province','City','Address','Name','Message','Tuition Plan','speciality');
  23. @columntype = ( 'NONE','NONE','MAIL','NONE','NONE','NONE','NONE','NONE','NONE','NONE','NONE');
  24. ###############################################################
  25. #              4.Section of controls                          #
  26. ###############################################################
  27. @controls = ( 'speciality','name','address','city','state','phone','fax','email');
  28. @controlvalues = ( '','','','','','','','');
  29. &get_control_values(@controls);
  30. ###############################################################
  31. #        5.Section of script body where    generated SQL      #
  32. #       statement template is to be placed with appropriate   #
  33. #                        insertions                           #
  34. ###############################################################
  35. @columnvalues = ( $controlvalues[6],$controlvalues[5],$controlvalues[7],"USA",$controlvalues[4],$controlvalues[3],$controlvalues[2],$controlvalues[1],"Hi","Cash",$controlvalues[0]);
  36. $stmt0 = "INSERT INTO Addresses(FaxNumber,WorkPhone,EmailAddress,Country,StateOrProvince,City,Address,FullName,Message,TuitionPlan,speciality)\n
  37.  VALUES ('\$controlvalues[6]','\$controlvalues[5]','\$controlvalues[7]','USA','\$controlvalues[4]','\$controlvalues[3]','\$controlvalues[2]','\$controlvalues[1]','Hi','Cash','\$controlvalues[0]')";
  38. ##################### End of prefix ###########################
  39.  
  40. #$test = FALSE;
  41. #####################Standard remainder of the script##########
  42. $useplatstr = "use "."$osplat"."::"."$dbplat";
  43. eval $useplatstr;
  44. use CGI ':standard';
  45. $header = "Header";
  46.  
  47. #################################################################
  48. #        7.Section of script body responsible                   #
  49. #             for input from stdio                              #
  50. #        and output to stdio footer's lines                     #
  51. #################################################################
  52. print header;
  53. #print   "<center><U><B><FONT SIZE=\"+5\">SQL TEST</FONT></B></U><center>";
  54. if (param()) {
  55.  }
  56.  
  57. #################################################################
  58. #        8.Section of script body responsible                   #
  59. #           for placing controls' values                        #
  60. #      in appropriate places in "controlvalues" array           #
  61. #      and placing received cursor in $cursor variable          #
  62. #################################################################
  63. $cursor = param('Cursor');
  64.  
  65. #################################################################
  66. #        9.Section of script body responsible                   #
  67. #           for placing controls' values                        #
  68. #      in appropriate places in "controlvalues" array           #
  69. #      and placing received cursor in $cursor variable          #
  70. #################################################################
  71. if($test){
  72. print '<FORM ACTION=';
  73. print $cgiurl;
  74. print 'METHOD=POST>';
  75. $colindex = 0;
  76. foreach $control (@controls){
  77.  print '<CENTER>';
  78.  $temp = $controls[$colindex];
  79.  print $temp ;
  80.  print '</CENTER><BR>';    
  81.  print '<CENTER>';
  82.  print '<INPUT TYPE="text" NAME="';
  83.  print $control;
  84.  print '"';
  85.  print ' VALUE="';
  86.  $temp = $controlvalues[$colindex];
  87.  print $temp;
  88.  print '" >';
  89.  print '</CENTER>';    
  90.  print '<BR >';
  91.  $colindex = $colindex + 1;
  92. }
  93. if($colindex){
  94. print '<BR><BR>';
  95. print '<CENTER>';
  96. print '<INPUT TYPE="Submit" NAME="submit"';
  97. print '</CENTER>';
  98. print '</FORM>';
  99. }
  100. }
  101.  
  102. #################################################################
  103. #        10.Section of script body where    generated SQL        #
  104. #statement template is to be placed with appropriate insertions #
  105. #     and   responsible  for getting  final sight  of SQL       #
  106. ################################################################# 
  107. $stmtval = "\$stmt"."= \""."$stmt0"."\"";
  108. eval  $stmtval;
  109.  
  110. $plat = "\$db"." = "."new "."$osplat"."::"."$dbplat"."("."$dbname".")";
  111. eval $plat;
  112. if (!$db){
  113.  &print_db_error;
  114.  return;
  115. }
  116. $stmtcount = $stmt;
  117. $_= $stmtcount;
  118. if($command EQ "UPDATE"){
  119. s#^$command#SELECT \* FROM #;
  120. s#SET.*##;
  121. }
  122. if($command EQ "DELETE"){
  123. s#^$command#SELECT \*#;
  124. }
  125. if($command NE "INSERT"){
  126. $stmtcount = $_;
  127. $rowtotal = &get_updated_records;
  128. }
  129. $lor = $db->Sql($stmt);
  130. if ($lor){
  131.  &print_sql_error;
  132.  return;
  133. }
  134. #################################################################
  135. #             11.Section of script body                         #
  136. #        responsible  for output "Query results" line           #
  137. #             along with total count of rows                    #
  138. #################################################################
  139. #### only for debugging purpose
  140. if($test){
  141. print "<HR>";
  142. print $stmtval,p;
  143. ####
  144. print "<HR>";
  145. }
  146. print   "<BR><BR>";
  147. print   "<center><U><B>QUERY RESULTS - $command command</B></U><center>";
  148. print   "<BR><BR>";
  149. #################################################################
  150. #             14.Section of script body                         #
  151. #        responsible  for output of table captions              #
  152. #            for query columns on feedback page                 #
  153. #################################################################
  154. if($command NE "DELETE"){
  155.  $capindex = 0;
  156.  foreach $caption (@captions){
  157.  $value = &get_converted_string($columnvalues[$capindex]);
  158.  print    "<B>$captions[$capindex] = $value</B><BR>";
  159.  $capindex = $capindex + 1;
  160.  }
  161. }
  162. if($command EQ "UPDATE"){
  163. print    "Updated $rowtotal records";
  164. }
  165. if($command EQ "DELETE"){
  166. print    "Deleted $rowtotal records";
  167. }
  168. if($pageURL NE ""){
  169.  print "<BR><A HREF =\"$pageURL\" TARGET=\"_top\">Go Back</A>";
  170. }
  171. print end_html;
  172. $db->Close();
  173.  
  174. ##################subroutins######
  175. sub get_updated_records
  176. {
  177. local($i)=0;
  178. $db->Sql($stmtcount);
  179. while($db->FetchRow()){
  180.  $i = $i+1;
  181. }
  182. local($rowtotal) = $i;
  183. return $i;
  184. }
  185. sub get_control_values
  186. {
  187.  local(@controls)=@_;
  188.  local($cindex) = 0;
  189.  foreach $control (@controls){
  190.   if(($t=param($control))){
  191.    if($controlvalues[$cindex] EQ 'NO'){
  192.     $controlvalues[$cindex] ='YES';
  193.    }
  194.    if($controlvalues[$cindex] EQ ''){
  195.     $t=~ s/'/''/g;
  196.     $t=~ s/\|//g;
  197.     $controlvalues[$cindex] =$t;
  198.    }
  199.   }
  200.   $cindex = $cindex + 1;
  201.  }
  202. }
  203. sub get_converted_string
  204. {
  205.   local($name) =@_;
  206.   $name=~ s/&/&/g;
  207.   $name=~ s/</</g;
  208.   $name=~ s/>/>/g;
  209.   $name=~ s/⌐/©/g;
  210.   $name=~ s/«/®/g;
  211.   $name=~ s/"/"/g;
  212.   $name=~ s/\n/<BR>/g;
  213.   $name=~ s/''/'/g;
  214.   return $name;
  215. }
  216.  
  217. sub print_db_error
  218. {
  219. print "<H1>DATASOURCE OPEN ERROR:</H1>";
  220. print "<P>No such DSN: <u>$dbname</u></P>";
  221. }
  222. sub print_sql_error
  223. {
  224.  print "<H1>QUERY EXECUTION ERROR:</H1>";
  225.  print "<P>QUERY STATEMENT: <U>$stmt</U></P>";
  226.  print "<P>DIAGNOSTICS: <U>";
  227.  print $db->Error();
  228.  print "</U></P>";
  229. }
  230.